/*
 * Copyright (c) 2012 RTCLab
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 */

package com.rtckit {

import flash.net.*;
import flash.events.*;
import flash.media.SoundTransform;

public class IncomingMedia extends Media {
	private var video:Boolean = false;

	function IncomingMedia(nc:NetConnection, config:SoftphoneConfig) {
		super(nc, config);
		config.reportVolumeChanged = reportVolumeChanged;
	}
	
	private function createStream(peerID:String = NetStream.CONNECT_TO_FMS):NetStream {
		var stream:NetStream = new NetStream(nc, peerID);
		stream.addEventListener(NetStatusEvent.NET_STATUS, this.netStatusHandler);
		stream.addEventListener(IOErrorEvent.IO_ERROR, this.streamErrorHandler, false, 0, true);
		stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this.streamErrorHandler, false, 0, true);
		stream.audioReliable = false;
		stream.videoReliable = false;
		stream.bufferTime = 0;
		stream.client = this;
		stream.soundTransform = new SoundTransform(config.getVolume() / 100);
		return stream;
	}
	
	private function reportVolumeChanged(volume:Number):void {
		if (serverStream)
			serverStream.soundTransform = new SoundTransform(volume / 100);
		if (peerStream)
			peerStream.soundTransform = new SoundTransform(volume / 100);
	}

	public function prepare():void {
		serverStream = createStream();
		serverStream.play(nc.farID);
	}
	
	public function connect(hasVideo:Boolean, peerID:String):void {
		if (peerID) {
			peerStream = createStream(peerID);
			peerStream.play("P2P" + peerID);
			if (config.video && hasVideo)
				config.videoContainer.switchToStream(peerStream);		
		} else {
			if (config.video && hasVideo)
				config.videoContainer.switchToStream(serverStream);
		}
		waitPeerConnection(peerID);
		video = hasVideo;
	}
	
	override public function onPeerConnectionFailed():void {
		if (config.video && video)
			config.videoContainer.switchToStream(serverStream);
		if (peerStream) {
			peerStream.close();
			peerStream = null;
		}
	}
	
	override public function currentStream():NetStream {
		return peerStream ? peerStream : serverStream;
	}
	
	public function disconnect():void {
		if (peerStream) {
			peerStream.close();
			peerStream = null;
		}
		
		if (serverStream) {
			serverStream.close();
			serverStream = null;
		}
		
		if (config.video)
			config.videoContainer.switchToStream(null);
	}
	
	public function destroy():void {
		
	}

	private function netStatusHandler(event:NetStatusEvent):void {
		var target:String = event.target == peerStream ? "P2P in" : "in";
		Logger.debug(target + ": " + event.info.code);
	}
	
	private function streamErrorHandler(event:ErrorEvent):void {
		var target:String = event.target == peerStream ? "P2P in" : "in";
		Logger.debug(target + ": " + event.type + ' ' + event.text);
	}
}

}